package top.xin1901.j2ecd.common.response;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

/**
 * @author HeYunjia
 */

@Data
@NoArgsConstructor
public class R {
    private Integer code;
    private String msg;
    private Map<String, Object> data;

    public static R getInstance() {
        return new R();
    }

    public static R success() {
        return new R(StatusEnum.SUCCESS);
    }

    public static R fail() {
        return new R(StatusEnum.FAIl);
    }

    public static R exception() {
        return new R(StatusEnum.EXCEPTION);
    }

    public R(StatusEnum status) {
        setCode(status.getCode());
        setMsg(status.getMsg());
    }

    public R code(Integer code) {
        this.setCode(code);
        return this;
    }

    public R msg(String msg) {
        this.setMsg(msg);
        return this;
    }

    public R add(String key, Object value) {
        if (data == null) data = new HashMap<>();
        data.put(key, value);
        return this;
    }
}
